var allrates;
var arates;
var brates;
var crates;
var drates;

function choosetype() {
    if ($("#selected").val() == 2) {
        $("#text-a").val("是");
        $("#text-a").attr("disabled", "false");
        $("#text-b").val("否");
        $("#text-b").attr("disabled", "false");
        $("#div-c").hide()
        $("#div-d").hide()
    } else {
        $("#text-a").val("");
        $("#text-a").removeAttrs("disabled");
        $("#text-b").val("");
        $("#text-b").removeAttrs("disabled");
        $("#div-c").show();
        $("#div-d").show();
    }
    checknotnull();
}

function allrate() {
    $("#div-id").hide();
    $("#indexModeltitle").html("添加问题");
    $("#all-rate").html("");
    $("#a-rate").html("");
    $("#b-rate").html("");
    $("#c-rate").html("");
    $("#d-rate").html("");
    $("#rate-test1").html("");
    $("#rate-test2").html("");

    $("#content").val("");
    $("#all-rate").val("");
    $("#text-a").val("");
    $("#text-b").val("");
    $("#text-c").val("");
    $("#text-d").val("");
    $("#a-rate").val("");
    $("#b-rate").val("");
    $("#c-rate").val("");
    $("#d-rate").val("");
    for (var i = 0; i <= 100; i++) {
        $("#all-rate").append("<option value='" + i + "'>" + i + "</option>");
    }
    checknotnull();
    choosetype();
}

function updatequestions(node) {
    $("#div-id").show();
    $("#input-id").val("");
    $("#all-rate").val("");
    $("#text-a").val("");
    $("#text-b").val("");
    $("#text-c").val("");
    $("#text-d").val("");
    $("#a-rate").val("");
    $("#b-rate").val("");
    $("#c-rate").val("");
    $("#d-rate").val("");
    $("#all-rate").html("");
    $("#a-rate").html("");
    $("#b-rate").html("");
    $("#c-rate").html("");
    $("#d-rate").html("");
    $("#rate-test1").html("");
    $("#rate-test2").html("");

    $("#indexModeltitle").html("修改问题");
    var id = $(node).parents("tr").find(".td-id").html();
    var content = $(node).parents("tr").find(".td-content").html();
    var type = $(node).parents("tr").find(".td-type").html();
    var scope = $(node).parents("tr").find(".td-scope").html();
    var answer = [];
    var rate = [];
    var index = 0;
    var inputindex = [5, 7, 9, 11];
    if (type == "选择题") {
        index = 4;
    } else {
        index = 2;
    }
    for (var i = 1; i < index + 1; i++) {
        var str = $(node).parents("tr").find("option").eq(i).html();
        answer.push(str.split(":")[0].split("\.")[1]);
        rate.push(str.split(":")[1].split("分")[0]);
    }


    $("#input-id").val(id);
    $("#content").val(content);

    for (var j = 0; j < 2; j++) {
        if ($("#selected").find("option").eq(j).html() == type) {
            $("#selected").find("option").eq(j).attr("selected", "true");
        } else {
            $("#selected").find("option").eq(j).removeAttrs("selected");
        }
    }
    for (var k = 0; k < 3; k++) {
        if ($("#scope").find("option").eq(k).html() == scope) {
            $("#scope").find("option").eq(k).attr("selected", "true");
        } else {
            $("#scope").find("option").eq(k).removeAttrs("selected");
        }
    }
    for (var m = 0; m < answer.length; m++) {
        $(".form-control").eq(inputindex[m]).val(answer[m]);
    }
    var ratea = parseInt(rate[0]);
    var rateb = parseInt(rate[1]);
    var ratec = parseInt(rate[2]);
    var rated = parseInt(rate[3]);
    var rateall = 0;
    if (type == "选择题") {
        rateall = ratea + rateb + ratec + rated;
    } else {
        rateall = ratea + rateb;
    }
    for (var i = 0; i <= 100; i++) {
        if (i == rateall) {
            $("#all-rate").append("<option value='" + i + "' selected>" + i + "</option>");
        } else {
            $("#all-rate").append("<option value='" + i + "'>" + i + "</option>");
        }
    }
    for (var i = 0; i <= rateall; i++) {
        if (i == ratea) {
            $("#a-rate").append("<option value='" + i + "' selected>" + i + "</option>");
        } else {
            $("#a-rate").append("<option value='" + i + "'>" + i + "</option>");
        }
    }
    for (var i = 0; i <= rateall - ratea; i++) {
        if (i == rateb) {
            $("#b-rate").append("<option value='" + i + "' selected>" + i + "</option>");
        } else {
            $("#b-rate").append("<option value='" + i + "'>" + i + "</option>");
        }
    }

    if (type == "选择题") {
        var ratec = parseInt(rate[2]);
        var rated = parseInt(rate[3]);
        for (var i = 0; i <= rateall - ratea - rateb; i++) {
            if (i == ratec) {
                $("#c-rate").append("<option value='" + i + "' selected>" + i + "</option>");
            } else {
                $("#c-rate").append("<option value='" + i + "'>" + i + "</option>");
            }
        }
        $("#d-rate").append("<option value='" + rated + "'>" + rated + "</option>");
    }
    if (type == "是非题") {
        $("#text-a").attr("disabled", "false");
        $("#text-b").attr("disabled", "false");
        $("#div-c").hide()
        $("#div-d").hide()
    } else {
        $("#text-a").removeAttrs("disabled");
        $("#text-b").removeAttrs("disabled");
        $("#div-c").show();
        $("#div-d").show();
    }
    checknotnull();
    $("#indexUpdateBtn").attr("disabled", "true");
}

function test() {
    alert("sssss")
}

function arate() {
    $("#a-rate").html("");
    $("#b-rate").html("");
    $("#c-rate").html("");
    $("#d-rate").html("");
    $("#rate-test").html("");
    allrates = $("#all-rate").val();
    for (var i = 0; i <= allrates; i++) {
        $("#a-rate").append("<option value='" + i + "'>" + i + "</option>");
    }
    checknotnull();
}

function brate() {
    $("#b-rate").html("");
    $("#c-rate").html("");
    $("#d-rate").html("");
    allrates = $("#all-rate").val();
    arates = $("#a-rate").val();
    brates = allrates - arates;
    for (var i = 0; i <= brates; i++) {
        $("#b-rate").append("<option value='" + i + "'>" + i + "</option>");
    }
    checknotnull();
}

function crate() {
    $("#c-rate").html("");
    $("#d-rate").html("");
    allrates = $("#all-rate").val();
    arates = $("#a-rate").val();
    brates = $("#b-rate").val();
    crates = allrates - arates - brates;
    for (var i = 0; i <= crates; i++) {
        $("#c-rate").append("<option value='" + i + "'>" + i + "</option>");
    }
    checknotnull();
}

function drate() {
    $("#d-rate").html("");
    allrates = $("#all-rate").val();
    arates = $("#a-rate").val();
    brates = $("#b-rate").val();
    crates = $("#c-rate").val();
    drates = allrates - arates - brates - crates;
    for (var i = 0; i <= drates; i++) {
        $("#d-rate").append("<option value='" + i + "'>" + i + "</option>");
    }
    checknotnull();
}

var a = false;

function checkrate() {
    drates = $("#d-rate").val();
    var sum = parseInt(arates) + parseInt(brates) + parseInt(crates) + parseInt(drates);
    var type = $("#selected").val();
    if (type == 2) {
        brates = $("#b-rate").val();
        sum = parseInt(arates) + parseInt(brates);
        if (sum < allrates) {
            $("#rate-test1").html("<span style='color: red'>分数合计应与总分相等！</span>");
            a = false;
        } else {
            $("#rate-test1").html("");
            a = true;
        }
    } else {
        if (sum < allrates) {
            $("#rate-test2").html("<span style='color: red'>分数合计应与总分相等！</span>");
            a = false;
        } else {
            $("#rate-test2").html("");
            a = true;
        }
    }
}

var b = false;

function checknotnull() {
    var content = $("#content").val();
    var allrate = $("#all-rate").val();
    var type = $("#selected").val();
    var scope = $("#scope").val();
    var textA = $("#text-a").val();
    var textB = $("#text-b").val();
    var textC = $("#text-c").val();
    var textD = $("#text-d").val();
    var rateA = $("#a-rate").val();
    var rateB = $("#b-rate").val();
    var rateC = $("#c-rate").val();
    var rateD = $("#d-rate").val();
    if (type == 2) {
        if (content != "" && allrate != "" && type != "" && scope != ""
            && textA != "" && textB != "" &&
            rateA != null && rateB != null) {
            checkrate();
            b = true;
            checkAB();
        } else {
            $("#indexUpdateBtn").attr("disabled", "true");
            b = false;
        }

    } else {
        if (content != "" && allrate != "" && type != "" && scope != ""
            && textA != "" && textB != "" && textC != "" && textD != "" &&
            rateA != null && rateB != null && rateC != null && rateD != null) {
            checkrate();
            b = true;
            checkAB()
        } else {
            $("#indexUpdateBtn").attr("disabled", "true");
            b = false;
        }
    }
}

function checkAB() {
    if (a && b) {
        $("#indexUpdateBtn").removeAttrs("disabled");
        a = false;
        b = false;
    } else {
        $("#indexUpdateBtn").attr("disabled", "true");
    }
}











